/**
 * 配置参考:
 * https://cli.vuejs.org/zh/config/
 */
const url = process.env.VUE_APP_BASE_URL
const CompressionWebpackPlugin = require("compression-webpack-plugin")
const productionGzipExtensions = ["js", "css"]
const TerserPlugin = require('terser-webpack-plugin') // 引入压缩插件
// 判断当前环境是否为生产环境
const isProductionTest = process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'test'
module.exports = {
  // 保存时是否保存 eslint 检查
  lintOnSave: false,
  // 其他配置项...
  chainWebpack: (config) => {
    const entry = config.entry("app")
    entry.add("babel-polyfill").end()
    entry.add("classlist-polyfill").end()
  },
  css: {
    // 忽略 CSS order 顺序警告
    // extract: { ignoreOrder: true }
  },
  configureWebpack: {
    performance: {
      // 打包后最大文件大小限制
      maxAssetSize: 1024 * 1000
    },
    output: {
      filename: "./js/[name].[hash:8].js"
    },
    optimization: {
      minimize: true, // 开启最小化
      minimizer: isProductionTest ? [
        new TerserPlugin({
          // TerserPlugin 配置
          terserOptions: {
            compress: true // 启用文件压缩
            // 其他 Terser 配置...
          }
        })
      ] : [],
      splitChunks: {
        cacheGroups: {
          chunks: isProductionTest ? 'all' : '',
          styles: {
            name: "styles",
            test: /\.css$/,
            chunks: "all",
            enforce: true
          },
          commons: {
            chunks: "initial",
            minChunks: 2,
            maxInitialRequests: 5,
            minSize: 0
          },
          vendor: {
            test: /node_modules/,
            chunks: "initial",
            name: "vendor",
            priority: 10,
            enforce: true
          }
        }
      }
    },
    // productionSourceMap: false,

    plugins: [
      new CompressionWebpackPlugin({
        filename: "[path].gz[query]",
        algorithm: "gzip",
        test: new RegExp("\\.(" + productionGzipExtensions.join("|") + ")$"),
        threshold: 1024, // 只有大小大于该值的资源会被处理,当前配置为对于超过1k的数据进行处理，不足1k的可能会越压缩越大
        minRatio: 0.99, // 只有压缩率小于这个值的资源才会被处理
        deleteOriginalAssets: false // 删除原文件
      })
    ],
    devtool: 'source-map'
  },
  publicPath: './',
  // 配置转发代理
  devServer: {
    disableHostCheck: true,
    port: 8080,
    proxy: {
      "/": {
        target: url,
        ws: false, // 需要websocket 开启
        pathRewrite: {
          "^/": "/"
        }
      }
    }
  }
}
